$(function(){
	$('input[placeholder]').placeholder();
			$("#pwd1").complexify({}, function(valid, complexity){
				if (!valid) {
					$('#complexity').animate({'width':complexity + '%'}).removeClass('valid').addClass('invalid');
				} else {
					$('#complexity').animate({'width':complexity + '%'}).removeClass('invalid').addClass('valid');
				}
				$('#complexity').html(Math.round(complexity) + '%');
			});
    $.validator.setDefaults({
        submitHandler: function(form){
            form.submit();
        }
    });
	$.validator.addMethod("alnum", function(value, element) {
      return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
     }, "只能包括英文字母和数字");
    $("#form").validate({
        errorPlacement: function(error, element){
            error.appendTo(element.parent());
        },
        rules: {
            "verifycode": {
                required: true,
				minlength:4,
				maxlength:4,
			     remote: {
                    url: "/myforum/checkVerifyCode", //后台处理程序
                    type: "get", //数据发送方式
                    dataType: "json", //接受数据格式
                    data: { //要传递的数据
                        verifycode: function(){
                            return $("#verifycode").val();
                        }
                    }
                }
            },
            "userid": {
                required: true,
				rangelength: [5, 15],
				alnum:true,
                remote: {
                    url: "/myforum/checkUserId", //后台处理程序
                    type: "get", //数据发送方式
                    dataType: "json", //接受数据格式
                    data: { //要传递的数据
                        userid: function(){
                            return $("#userid").val();
                        }
                    }
                }
            },
            "pwd1": {
                required: true,
                minlength: 6,
                maxlength: 20
            },
            "pwd2": {
                required: true,
                equalTo: "#pwd1"

            },
            "email": {
                required: true,
                email: true,
				remote: {
                    url: "/myforum/checkEmail", //后台处理程序
                    type: "get", //数据发送方式
                    dataType: "json", //接受数据格式
                    data: { //要传递的数据
                        userid: function(){
                            return $("#email").val();
                        }
                    }
                }
            },
            "birth": {
                date: true
            },
            "icq": {
                rangelength: [5, 12],
                digits: true
            },
            "webpage": {
                url: true
            }
        },
        messages: {
            "verifycode": {
                required: "验证码必填！",
				minlength:"验证码为4位!",
				maxlength:"验证码为4位!",
				remote:"验证码不正确！"
            },
            "userid": {
                required: "用户名必填！",
				rangelength: "用户名输入长度必须介于 {0}和 {1} 之间",
				alnum:"只能包括英文字母和数字",
                remote:"用户名已经被注册!"
            },
            "pwd1": {
                required: "密码必填！",
                minlength: $.validator.format("密码不得短于{0}位"),
                maxlength: $.validator.format("密码不得长于{0}位")
            },
            "pwd2": {
                required: "确认密码必填！",
                equalTo: "请输入相同的密码"
            },
            "email": {
                required: "email必填！",
                email: "请输入正确的email！",
				remote:"邮箱已被其他人使用！"
            },
            "birth": {
                date: "必须输入正确格式的日期"
            },
            "icq": {
                rangelength: "qq号输入长度必须介于 {0}和 {1} 之间",
                digits: "qq号必须为数字"
            },
            "webpage": {
                url: "请输入合法的网址"
            }
        }
    });
    $("#pwd2").change(function(){
        $("#pwd").val($("#pwd1").val());
    });
    $("#advshow").click(function(){
        if ($("#advshow").attr("checked") === "checked") {
            $("#adv").fadeIn();
        }
        else {
            $("#adv").fadeOut();
        }
    });
    $("#regsubmit").hide();
    $('#kaptchaImage').click(function(){ // 生成验证码
        $(this).hide().attr('src', '/captcha-image?' + Math.floor(Math.random() * 100)).fadeIn();
    }).attr("style", "cursor:pointer");
    $("#birth").datepicker({
        changeMonth: true,
        changeYear: true,
        dateFormat: 'yy-mm-dd',
		maxDate:new Date()
    });
    $("#protocol").click(function(){
        if ($("#protocol").attr("checked") === "checked") {
            $("#regsubmit").show();
        }
        else {
            $("#regsubmit").hide();
        }
    });
    $("#link").click(function(){
        $("#checkValue").val("1");
        if ($("#protocol").attr("checked") === "checked") {
            $("#regsubmit").show();
        }
    });
    $("#email").mailAutoComplete({
        boxClass: "out_box", // 外部box样式
        listClass: "list_box", // 默认的列表样式
        focusClass: "focus_box", // 列表选样式中
        markCalss: "mark_box", // 高亮样式
        autoClass: false,
        textHint: true, // 提示文字自动隐藏
        hintText: "请输入邮箱地址"
    });
    // $("#city").hide();
    $.initProv("#ddlProvince", "#ddlCity", "请选择所在省份", "请选择所在城市");
    $("#ddlCity").change(function(){
        if ($("#ddlCity").val() !== "-1") {
            if ($("#ddlProvince").find("option:selected").text().indexOf("市") !== -1) {
                $("#city").val($("#ddlCity").find("option:selected").text());
            }
            else {
                $("#city").val($("#ddlProvince").find("option:selected").text() + $("#ddlCity").find("option:selected").text());
            }
        }
    });
});
